package com.example.glidelearningproject.fragment.start;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.fragment.app.DialogFragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.glidelearningproject.GlideApp;
import com.example.glidelearningproject.R;
import com.xuexiang.xpage.annotation.Page;
import com.xuexiang.xpage.base.XPageFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

@Page(name = "Glide在RecyclerView中的使用")
public class RecyclerViewFragment extends XPageFragment {

    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;

    private MyRecyclerViewAdapter adapter;
    private List<String> imageUrls = new ArrayList<>();

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_recyclerview;
    }

    @Override
    protected void initViews() {
        imageUrls.add("https://oscimg.oschina.net/oscnet/up-ca715719a1c2f1f1c490fe082f160d629f0.JPEG");
        imageUrls.add("https://oscimg.oschina.net/oscnet/up-79537029810bf2d3ee5f81e4c38e36acd11.png");
        imageUrls.add("https://oscimg.oschina.net/oscnet/up-08547cb3406ea62b880577e3c5fdbc4a26e.JPEG");
        imageUrls.add("https://oscimg.oschina.net/oscnet/up-5cf65fb8a4567ef34d926e7038d27800ee2.JPEG");
        imageUrls.add("https://oscimg.oschina.net/oscnet/up-8924b0fc5994bad6c9ea82e8a7de841e5dd.JPEG");
        imageUrls.add("https://oscimg.oschina.net/oscnet/up-da101de75e53611cfb075436f30c250addb.JPEG");
        imageUrls.add("https://static.oschina.net/uploads/space/2021/0323/080820_SwxX_4937141.jpeg");
        imageUrls.add("https://oscimg.oschina.net/oscnet/up-bd278d23fa9dd1a4a6fd226c1abf0f8ef34.png");
        imageUrls.add("https://oscimg.oschina.net/oscnet/up-4bd22032641bd26481bd8b4653fcc7148e6.png");
        adapter = new MyRecyclerViewAdapter(getContext(),imageUrls);
        recyclerView.setLayoutManager(new GridLayoutManager(getContext(),3));
        recyclerView.setAdapter(adapter);
    }

    @Override
    protected void initListeners() {

    }

    public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.ViewHolder>{

        private List<String> imageUrls;
        private Context context;

        public MyRecyclerViewAdapter(Context context,List<String> imageUrls) {
            this.imageUrls = imageUrls;
            this.context = context;
        }

        @NonNull
        @Override
        public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(context).inflate(R.layout.adapter_imageview_item,parent,false);
            return new ViewHolder(view);
        }

        @Override
        public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
            String url = imageUrls.get(position);
            Log.e("url",url);
            Glide.with(context).load(url).into(holder.imageView);
        }

        @Override
        public int getItemCount() {
            return this.imageUrls.size();
        }

        public class ViewHolder extends RecyclerView.ViewHolder {
            ImageView imageView;
            public ViewHolder(@NonNull View itemView) {
                super(itemView);
                imageView = itemView.findViewById(R.id.imageView);
            }
        }
    }
}
